﻿using System;
using System.Resources;
using System.Globalization;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Portal.Environment;
using Portal.Language;

using System.Text;
using System.Text.RegularExpressions;

public partial class ChangePassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);
        Portal.Environment.Environments.SetEnvironment();
        AjaxPro.Utility.RegisterTypeForAjax(typeof(ChangePassword), this.Page);
        if (Environments.CurrentLoginUser == null)
        {
            Response.Redirect("login.aspx");
        }

        string HtmlAddText =
                "<div class=cssFormLogin>" +
                "<div class=\"box_380\">" +
                "   <div class=\"box_title_380\">" + ResourceBLL.GetText("Home", "FormTitleChange", "Đổi mật khẩu sử dụng") + "</div>" +
                "   <div class=\"box_body_380\">" +
                "        <table>" +
                "            <tr>" +
                "                <td class=cssLoginTd1>" + ResourceBLL.GetText("Home", "Account", "Tài khoản") + "</td>" +
                "                <td class=cssLoginTd2>" + Environments.CurrentLoginUser.LoginName + " / " + Environments.CurrentLoginUser.FullName+ "</td>" +
                "            </tr>" +
                "            <tr>" +
                "                <td class=cssLoginTd1>" + ResourceBLL.GetText("Home", "OldPassword", "Mật khẩu cũ") + "</td>" +
                "                <td><input type=password id=txtOldPassword class=cssPassword></td>" +
                "            </tr>" +
                "            <tr>" +
                "                <td class=cssLoginTd1>" + ResourceBLL.GetText("Home", "Password", "Mật khẩu") + "</td>" +
                "                <td><input type=password id=txtPassword class=cssPassword></td>" +
                "            </tr>" +
                "            <tr>" +
                "                <td class=cssLoginTd1>" + ResourceBLL.GetText("Home", "RePassword", "Nhập lại") + "</td>" +
                "                <td><input type=password id=txtRePassword class=cssPassword></td>" +
                "            </tr>" +
                "            <tr>" +
                "                <td class=cssLoginTd1></td>" +
                "                <td><input type=button class=cssAcceptLogin onclick=javascript:ClientDoChangePasword(); value=\"" + ResourceBLL.GetText("Home", "DoChangePassword", "Thay đổi") + "\"><input onclick=\"javascript:window.open('default.aspx','_self');\" type=button class=cssCancelLogin value=\"" + ResourceBLL.GetText("Home", "Back", "Quay lại") + "\"></td>" +
                "            </tr>" +
                "        </table>" +
                "    </div>" +
                "<div class=\"box_bottom_380\"></div>" +
                "<div>" +
                "</div>";
        placeHolderContent.Controls.Clear();
        placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(HtmlAddText));
        Session["ActiveTheme"] = Environments.GetAppSettings("ActiveTheme");
    }

    [AjaxPro.AjaxMethod]
    public string ServerSideChangePassword(string OldPassword,string Password,string RePassword)
    {
        try
        {
            Environments.CheckSessionTimeOut(true);
            
            if (!Password.Equals(RePassword)) throw new Exception(ResourceBLL.GetText("Home", "RePasswordError", "Mật khẩu nhập lại không khớp!"));
            string Pasword = Environments.CurrentLoginUser.Password;
            OldPassword = new RSACSPCls().Encrypt("vvvxxx!@#$abc*()", OldPassword);
            if (!Pasword.Equals(OldPassword ))
            {
                throw new Exception(ResourceBLL.GetText("Home","OldPasswordIncorrect","Mật khẩu cũ nhập không đúng!"));
            }

            Portal.Environment.Permission.ChangePassword(Environments.CurrentLoginUser.LoginName, Password, RePassword);
            Environments.CurrentLoginUser.Password = new RSACSPCls().Encrypt("vvvxxx!@#$abc*()", Password);
            return "OK";
        }
        catch (Exception ex)
        {
            return "ERROR: " + ex.Message.ToString();
        }
    }

    [AjaxPro.AjaxMethod]
    public string GetUserBackendAspx()
    {
        try
        {
            Portal.Environment.Environments.CurrentLoginUser = Portal.Environment.Environments.CurrentLoginUser;
            if (Portal.Environment.Environments.CurrentLoginUser == null)
            {
                return "default.aspx";
            }
            else
            {
                string Temp = Portal.Environment.Environments.CurrentLoginUser.UserBackendAspx;
                if (Temp.Trim().Length == 0) Temp = "default";
                return Temp + ".aspx";
            }
        }
        catch (Exception ex)
        {
            return "ERROR: " + ex.Message.ToString();
        }
    }
}
